home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr20 / spr_cut.zip / CUTMAIN.C < prev    next >
C/C++ Source or Header  |  1995-03-13  |  7KB  |  331 lines

  1. /* ---------------------------------------------------- */
  2. /* SPRITE CUTTER UTILITY v2.0                           */
  3. /* by gaggi@cs.unibo.it                                 */
  4. /* ---------------------------------------------------- */
  5. /* CUTMAIN.C                                            */
  6. /* Main module                                          */
  7. /* ---------------------------------------------------- */
  8.  
  9. #include <stdio.h>
  10. #include <conio.h>
  11. #include <alloc.h>
  12. #include <io.h>
  13. #include <fcntl.h>
  14. #include <dos.h>
  15. #include <string.h>
  16.  
  17. #include "mouse.h"
  18. #include "graph13h.h"
  19.  
  20. unsigned _heaplen= 8*1024;    /* 8Kb of near heap */
  21. unsigned _stklen=  5*1024;    /* 5Kb of stack     */
  22.  
  23. /* ---------------------------------------------------- */
  24.  
  25. #define MOUSE_COLOR     0x04    /* mouse pointer color */
  26.  
  27. #define CRSR_UP         72
  28. #define CRSR_DOWN       80
  29. #define CRSR_RIGHT      77
  30. #define CRSR_LEFT       75
  31.  
  32. #define SAVE_SIZE       42*8*16 + 2*sizeof(unsigned)
  33.  
  34. #include "interfac.h"
  35. #include "cut_menu.h"
  36. #include "cutlst.h"
  37. #include "picview.h"
  38.  
  39. /* ---------- prototypes of generic functions --------- */
  40.  
  41. extern void xor_rect(unsigned, unsigned, unsigned, unsigned, unsigned);
  42. /* Xors a rectangle on the screen */
  43.  
  44. extern void put_arrow(unsigned, unsigned);
  45. /* Puts the mouse pointer (an arrow) on the screen */
  46.  
  47. extern void animate();
  48.  
  49.  
  50. /* ---------- static datas ---------------------------- */
  51.  
  52. static char greetings[]=
  53.         "Thank you for using my software, hope you enjoyed it!\n"
  54.         "Feel free to send any comment to: gaggi@cs.unibo.it";
  55.  
  56. static char logo[]= " Sprite Cutter v2.0 \r"
  57.               " by gaggi@cs.unibo.it ";
  58.  
  59. static char *error_msg[]= {
  60.                    "Memory allocation fault\r",
  61.                    "Sorry, can't find:\r%s",
  62.                    "Error while loading ...\r",
  63.                    "Source picture\rnot loaded",
  64.                    "Mouse not found."
  65.                   };
  66.  
  67. /* ---------- Functions ------------------------------- */
  68.  
  69. void Arrow_cursor()
  70. /* Function for the drawing of the mouse pointer during */
  71. /* the menu and the choice of animation frames.         */
  72. {
  73.  static unsigned char first_time= 1;
  74.  static unsigned char cursor_area[68];
  75.  register unsigned MouseX= Mouse.PointX >> 1,
  76.              MouseY= Mouse.PointY;
  77.  
  78.  asm push bx
  79.  asm push cx
  80.  asm push dx
  81.  
  82.  if (first_time) {
  83.             save_area(MouseX, MouseY, 8, 8, cursor_area);
  84.             put_arrow(MouseX, MouseY);
  85.             first_time--;
  86.              }
  87.  else {
  88.      put_area(MouseX, MouseY, cursor_area);
  89.      first_time++;
  90.     };
  91.  
  92.  asm pop  dx
  93.  asm pop  cx
  94.  asm pop  bx
  95. }
  96.  
  97.  
  98. void cut_cursor()
  99. /* Function for the drawing of the mouse cursor */
  100. {
  101.  asm push bx
  102.  asm push cx
  103.  asm push dx
  104.  
  105.  xor_rect(Mouse.PointX >> 1, Mouse.PointY, cut_x_size, cut_y_size, 0x0f);
  106.  
  107.  asm pop  dx
  108.  asm pop  cx
  109.  asm pop  bx
  110. }
  111.  
  112.  
  113. void error(unsigned err_no, char *info)
  114. {
  115.  char *buffer_1;
  116.  
  117.  if ( info && (buffer_1= (char *)malloc(50)) )
  118.     {
  119.      sprintf(buffer_1, error_msg[err_no - 1], info);
  120.      Message_box(buffer_1, 40, 80, 0);
  121.      free(buffer_1);
  122.     }
  123.  else Message_box(error_msg[err_no - 1], 40, 80, 0);
  124. }
  125.  
  126. static void Load_font(char *filename)
  127. {
  128.  char buffer[80];
  129.  register int handle;
  130.  register char *Font;
  131.  
  132.  strcpy(buffer, filename);
  133.  if ( Font= strrchr(buffer, '\\') ) Font++;
  134.  else Font= buffer;
  135.  
  136.  *Font= 0;
  137.  strcat(buffer, "cutter.fnt");
  138.  
  139.  if ( (handle= open(buffer, O_RDONLY | O_BINARY)) == -1 ||
  140.      !(Font= (char *)malloc(1024)) ) return;
  141.  
  142.  read(handle, Font, 1024);
  143.  close(handle);
  144.  
  145.  CurFont= (char far *)Font;
  146. }
  147.  
  148.  
  149. void user_def_size()
  150. /* Function for changing the size of the cut */
  151. {
  152.  register unsigned done= 0;
  153.  char temp[23];
  154.  static char msg1[]= "Arrows to change size, ENTER when done.";
  155.  static char status[]= "x: %03d y: %03d";
  156.  
  157.  Curcol= 0;
  158.  Draw_box(0, 0, 320, 130);
  159.  
  160.  Curcol= 15;
  161.  Draw_rect(0, 0, 320, 130);
  162.  
  163.  Curcol= 7;
  164.  OutText(2, 2, msg1);
  165.  xor_rect(20, 20, cut_x_size, cut_y_size, 0x07);
  166.  
  167.  do
  168.  {
  169.   sprintf(temp, status, cut_x_size, cut_y_size);
  170.  
  171.   Curcol= 7;
  172.   OutText(2, 10, temp);
  173.  
  174.   if ( kbhit() )
  175.      {
  176.     switch ( getch() )
  177.     {
  178.      case 0:
  179.           xor_rect(20, 20, cut_x_size, cut_y_size, 0x07);
  180.  
  181.           switch ( getch() )
  182.           {
  183.            case CRSR_UP:
  184.               if (--cut_y_size < 3) cut_y_size++;
  185.               break;
  186.  
  187.            case CRSR_DOWN:
  188.               if (++cut_y_size > 100) cut_y_size--;
  189.               break;
  190.  
  191.            case CRSR_LEFT:
  192.               if (--cut_x_size < 3) cut_x_size++;
  193.               break;
  194.  
  195.            case CRSR_RIGHT:
  196.               if (++cut_x_size > 100) cut_x_size--;
  197.           };
  198.  
  199.           xor_rect(20, 20, cut_x_size, cut_y_size, 0x07);
  200.           Curcol= 0;
  201.           Draw_box(2, 10, 200, 8);
  202.           break;
  203.  
  204.      case '\r':
  205.           done++;
  206.     };
  207.  
  208.      };
  209.  
  210.  } while (!done);
  211.  
  212.  if (*source_name) {
  213.               Show_picture(&source_pic, 0);
  214.               Draw_cuts();
  215.              }
  216.  else {
  217.      Curcol= 0;
  218.      Draw_box(0, 0, 320, 130);
  219.     };
  220.  
  221.  setlocation(0, 0);
  222. }
  223.  
  224. /* ---------- Principale ----------------------------- */
  225.  
  226. int main(unsigned argc, char *argv[])
  227. {
  228.  register unsigned done= 0;
  229.  
  230.  /* Initial settings */
  231.  if ( init_PIC(&source_pic) ) {
  232.                      puts(*error_msg);
  233.                      return -1;
  234.                     };
  235.  set_video(MODE_13H);
  236.  
  237.  if ( !resetmouse() ) {
  238.                  set_video(TEXT_MODE);
  239.                  puts(error_msg[4]);
  240.                  return -1;
  241.                 };
  242.  
  243.  Install_mouse();
  244.  set_Xrange(0, (320 - cut_x_size) << 1); /* Redefining the range of  mouse */
  245.  set_Yrange(0, (200 - cut_y_size) );
  246.  
  247.  Load_font(*argv);
  248.  
  249.  SetDrawProc((void *)cut_cursor);      /* Mouse cursor drawing procedure */
  250.  mouse_cursor(ON);
  251.  
  252.  Message_box(logo, 50, 80, 0);
  253.  
  254.  if ( argc > 1 ) load_source(argv[1]); /* Load source picture if present */
  255.                      /* its name is given as command   */
  256.                            /* line parameter.                */
  257.  do
  258.  {
  259.   switch ( Mouse.Button ) /* Looking at mouse control structure */
  260.   {
  261.    case LEFT_BUTTON:      /* if left button is pressed cut a sprite */
  262.       if ( done= cut_sprite(Mouse.EventX >> 1, Mouse.EventY) )
  263.          error(done, NULL);
  264.       done= 0;
  265.       break;
  266.  
  267.    case RIGHT_BUTTON:     /* if right button is pressed perform main menu */
  268.       set_Xrange(0, 639);
  269.       set_Yrange(0, 199);
  270.       switch ( Do_menu(&main_menu, Mouse.EventX >> 1, Mouse.EventY) )
  271.       {
  272.        case CMD_SOURCE:
  273.           load_source(NULL);
  274.           break;
  275.  
  276.        case CMD_SPRITE:
  277.           switch (Do_menu(&sprite_menu, Mouse.EventX >> 1, Mouse.EventY))
  278.           {
  279.            case CMD_LOADLIST:
  280.               load_LST();
  281.               break;
  282.  
  283.            case CMD_SAVELIST:
  284.               save_LST(0);
  285.               break;
  286.  
  287.            case CMD_ANIMATE :
  288.               if ( cut_list ) animate();
  289.               break;
  290.  
  291.            case CMD_SAVESPR :
  292.               save_SPR();
  293.           };
  294.           break;
  295.  
  296.        case CMD_SIZE  :
  297.           switch (done= Do_menu(&size_menu, Mouse.EventX >> 1, Mouse.EventY))
  298.           {
  299.            case CMD_USERDEF:
  300.               user_def_size();
  301.            case CMD_NULL:
  302.               break;
  303.            default:
  304.             cut_x_size= done;
  305.             cut_y_size= done;
  306.           };
  307.           done= 0;
  308.           break;
  309.  
  310.        case CMD_QUIT  :
  311.           save_LST(1);  /* Save list if it has been modified */
  312.           done++;
  313.       };
  314.       set_Xrange(0, (320 - cut_x_size) << 1);
  315.       set_Yrange(0, 200 - cut_y_size);
  316.       SetDrawProc((void *)cut_cursor);
  317.       mouse_cursor(ON);
  318.   };
  319.  
  320.  } while(!done);
  321.  
  322.  /* Final housekeeping ... and goodbye! */
  323.  resetmouse();
  324.  if (CurFont != DEFAULT_FONT) free((char *)CurFont);
  325.  PIC_free(&source_pic);
  326.  set_video(TEXT_MODE);
  327.  puts(greetings);
  328.  
  329.  return 0;
  330. }
  331.